﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using DotNetNuke;
using DotNetNuke.Security;

namespace Tatuo.Modules.RefrigeratorDoor
{
    public partial class View : DotNetNuke.Entities.Modules.PortalModuleBase
    {
        private DateTime thisDate;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                thisDate = DateTime.Now;
                this.calSelector.SelectedDate = Convert.ToDateTime(thisDate.ToShortDateString());
                this.calSelector.DataBind();
                this.lblSelectedDate.Text = thisDate.ToString("D");
                
            }

            this.mvEventCalendar.SetActiveView(vwCalendarDetails);

            if (!(this.UserInfo.IsSuperUser || PortalSecurity.IsInRole("Administrators")))
            {
                this.grdCalendarDetails.Columns[0].Visible = false;
                this.btnShowEventEntry.Visible = false;

                this.grdToDoList.Columns[0].Visible = false;
                this.frmToDoEntry.Visible = false;

                this.grdShoppingList.Columns[0].Visible = false;
                this.frmShoppingEntry.Visible = false;
            }
        }

        protected void calSelector_SelectionChanged(object sender, EventArgs e)
        {
            this.lblSelectedDate.Text = calSelector.SelectedDate.ToString("D");
            this.mvEventCalendar.SetActiveView(vwCalendarDetails);
            this.grdCalendarDetails.DataBind();
        }
        protected void btnShowEventEntry_Click(object sender, EventArgs e)
        {
            this.mvEventCalendar.SetActiveView(vwCalendarEntry);
            this.frmCalendarEntry.ChangeMode(FormViewMode.Insert);
        }
        protected void ldsCalendarDetails_Inserting(object sender, LinqDataSourceInsertEventArgs e)
        {
            RefrigeratorTask RefrigeratorTask = (RefrigeratorTask)e.NewObject;

            RefrigeratorTask.UserID = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo().UserID;
            RefrigeratorTask.ModuleID = ModuleId;
            RefrigeratorTask.TaskType = "Calendar";

            try
            {
                RefrigeratorTask.StartDate = RefrigeratorTask.ParsedStartDate;
                RefrigeratorTask.StopDate = RefrigeratorTask.ParsedStopDate;
            }
            catch (Exception)
            {
                // NEED ERROR MSG "DATES NOT VALID"
                // e.Cancel = true;
            }
        }
        protected void ldsCalendarDetails_Inserted(object sender, LinqDataSourceStatusEventArgs e)
        {
            this.grdCalendarDetails.DataBind();
            this.mvEventCalendar.SetActiveView(vwCalendarDetails);
        }
        protected void btnCancelEvent_Click(object sender, EventArgs e)
        {
            this.mvEventCalendar.SetActiveView(vwCalendarDetails);
        }
        protected void ldsToDoList_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            e.WhereParameters["ModuleId"] = ModuleId;
        }
        protected void ldsToDoList_Inserting(object sender, LinqDataSourceInsertEventArgs e)
        {
            RefrigeratorTask RefrigeratorTask = (RefrigeratorTask)e.NewObject;

            RefrigeratorTask.UserID = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo().UserID;
            RefrigeratorTask.ModuleID = ModuleId;
            RefrigeratorTask.TaskType = "ToDo";
        }
        protected void ldsToDoList_Inserted(object sender, LinqDataSourceStatusEventArgs e)
        {
            this.grdToDoList.DataBind();
        }
        protected void ldsShoppingList_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            e.WhereParameters["ModuleId"] = ModuleId;
        }
        protected void ldsShoppingList_Inserting(object sender, LinqDataSourceInsertEventArgs e)
        {
            RefrigeratorTask RefrigeratorTask = (RefrigeratorTask)e.NewObject;

            RefrigeratorTask.UserID = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo().UserID;
            RefrigeratorTask.ModuleID = ModuleId;
            RefrigeratorTask.TaskType = "Shopping";
        }
        protected void ldsShoppingList_Inserted(object sender, LinqDataSourceStatusEventArgs e)
        {
            this.grdShoppingList.DataBind();
        }
        protected void ldsCalendarGrid_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            // get DateTime object exactly one second before midnight this morning
            DateTime yesterday = DateTime.Parse(this.calSelector.SelectedDate.ToString("d"));
            yesterday = yesterday.AddSeconds(-1);
            // get DateTime object exactly one second after midnight tonight
            DateTime tomorrow = DateTime.Parse(this.calSelector.SelectedDate.AddDays(1).ToString("d"));
            tomorrow = tomorrow.AddSeconds(1);

            RefrigeratorDoorDataContext RefrigeratorDoorDataContext = new RefrigeratorDoorDataContext();

            var Results = from CalendarItems in RefrigeratorDoorDataContext.RefrigeratorTasks
                          where (CalendarItems.ModuleID == ModuleId &&
                                 CalendarItems.TaskType == "Calendar" &&
                                 CalendarItems.StartDate > yesterday &&
                                 CalendarItems.StopDate < tomorrow)
                          select CalendarItems;

            e.Result = Results;
        }
        protected void grdCalendarDetails_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.frmCalendarEntry.ChangeMode(FormViewMode.Edit);
            this.frmCalendarEntry.DataBind();
            this.mvEventCalendar.SetActiveView(vwCalendarEntry);
        }
        protected void ldsCalendarDetails_Updated(object sender, LinqDataSourceStatusEventArgs e)
        {
            this.grdCalendarDetails.DataBind();
            this.mvEventCalendar.SetActiveView(vwCalendarDetails);
        }
        protected void ldsCalendarDetails_Updating(object sender, LinqDataSourceUpdateEventArgs e)
        {
            RefrigeratorTask RefrigeratorTask = (RefrigeratorTask)e.NewObject;

            try
            {
                RefrigeratorTask.StartDate = RefrigeratorTask.ParsedStartDate;
                RefrigeratorTask.StopDate = RefrigeratorTask.ParsedStopDate;
            }
            catch (Exception)
            {
                // NEED ERROR MSG "DATES NOT VALID"
                // e.Cancel = true;
            }
        }

        protected void frmCalendarEntry_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            if (!(e.Values["StartDay"] == null))
            {
                this.calSelector.SelectedDate = Convert.ToDateTime((string)e.Values["StartDay"]);
                this.calSelector.DataBind();
                this.grdCalendarDetails.DataBind();
                this.lblSelectedDate.Text = Convert.ToDateTime(e.Values["StartDay"]).ToString("D");
            }
        }
        protected void frmCalendarEntry_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            if (!(e.NewValues["StartDay"] == null))
            {
                this.calSelector.SelectedDate = Convert.ToDateTime((string)e.NewValues["StartDay"]);
                this.calSelector.DataBind();
                this.grdCalendarDetails.DataBind();
                this.lblSelectedDate.Text = Convert.ToDateTime(e.NewValues["StartDay"]).ToString("D");
            }
        }

        // http://msdn.microsoft.com/en-us/library/ms228044(VS.85).aspx

        protected void HighlightCurrentEvents()
        {
            DateTime begDate = new DateTime(calSelector.VisibleDate.Year, calSelector.VisibleDate.Month, 1);
            DateTime endDate = begDate.AddMonths(1);
        }




        protected void calSelector_DayRender1(object sender, DayRenderEventArgs e)
        {
            RefrigeratorDoorDataContext RefrigeratorDoorDataContext = new RefrigeratorDoorDataContext();

            var Results = from CalendarItems in RefrigeratorDoorDataContext.RefrigeratorTasks
                          where (CalendarItems.ModuleID == ModuleId &&
                                 CalendarItems.TaskType == "Calendar" &&
                                 CalendarItems.StartDate >= e.Day.Date &&
                                 CalendarItems.StopDate < e.Day.Date.AddDays(1))
                          select CalendarItems;

            if (Results.Count() > 0)
            {
                e.Cell.BackColor = System.Drawing.Color.Turquoise;
            }
        }
        protected void calSelector_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
        {
            calSelector.DataBind();
        }
}
}